﻿namespace GoodAdmin.Core;

public static class DataTableExtensions
{
    public static List<T> ToList<T>(this DataTable table) where T : new()
    {
        List<T> list = new List<T>();

        foreach (DataRow row in table.Rows)
        {
            T item = new T();

            foreach (PropertyInfo prop in typeof(T).GetProperties())
            {
                if (!table.Columns.Contains(prop.Name)) continue;

                object value = row[prop.Name];

                if (value == DBNull.Value) value = null;

                prop.SetValue(item, value);
            }

            list.Add(item);
        }

        return list;
    }
}

